<!DOCTYPE html>
<html lang="en">
<title>私教教练列表</title>
<#import "/layout/layout.html" as body/>
<@body.page>
<style>
    .dataTables_empty {
        display: none
    }
</style>

<!--<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>教练管理</h2>
        <ol class="breadcrumb">
            <li><a href="#">教练管理</a></li>
            <li class="active"><strong>教练列表</strong></li>
        </ol>
    </div>
</div>-->
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5 class="pull-left">私教教练列表</h5>
                </div>
                <div class="ibox-content is-drag">
                    <div class="table-responsive">
                        <form class="form-inline" role="form">

                            <div class="form-group">
                                <label for="id">教练 ID：</label>
                                <input class="form-control" type="text" id="id" name="id" placeholder="输入教练ID" />
                            </div>

                            <div class="form-group">
                                <label for="nickName">姓名：</label>
                                <input class="form-control" type="text" id="nickName" name="nickName" placeholder="输入教练名字" />
                            </div>

                            <div class="form-group">
                                <label for="mobile">手机：</label>
                                <input class="form-control" type="text" id="mobile" name="mobile" placeholder="输入教练手机号码" />
                            </div>

                            <div class="form-group margin-left">
                                <label>门店：</label>
                                <select class="form-control" id="storeId" name="storeIdQuery">
                                    <option value="0">全部</option>
                                    <#if stores??>
                                        <#list stores as store>
                                            <option value="${store.id}">${store.storeName}</option>
                                        </#list>
                                    </#if>
                                </select>
                            </div>
                            <div class="form-group margin-left">
                                <label>教练类型：</label>
                                <select class="form-control" id="workType" name="workType">
                                    <option value="">全部</option>
                                    <option value="1">全职</option>
                                    <option value="2">兼职</option>
                                </select>
                            </div>
                            <div class="form-group margin-left">
                                <label>状态：</label>
                                <select class="form-control" id="status" name="status">
                                    <option value="">全部</option>
                                    <option value="1">在职</option>
                                    <option value="2">离职</option>
                                </select>
                            </div>
                            <button type="button" class="btn btn-outline btn-primary" style="margin-top: 5px" onClick="coachPage(1)">确认</button>
                        </form>
                        <hr>
                        <table class="table table-hover table-center dataTables-example">
                            <thead>
                            <tr>
                                <th>序号</th>
                                <th>教练ID</th>
                                <th>教练姓名</th>
                                <th>教练手机</th>
                                <th>教练类型</th>
                                <th>加入时间</th>
                                <th>所属门店</th>
                                <th>在职</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody id="coachList">
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="ibox-footer overflow">
                    <h4 class="pull-left">合计：<span id="coachCount">0</span>位教练</h4>
                    <button type="button" onclick="sort('coachList')" class="btn btn-success pull-right">确定修改排序</button>
                </div>

            </div>
        </div>
    </div>
</div>

<script>

    //教练编辑
    function editCoach(id) {
        window.location = "${baseUrl}system/coach/to_coach_edit?id=" + id;
    }

    $(document).ready(function () {
        //导出excel
        ExampleFile("教练excel");

        coachPage(1);

        $(document).keypress(function (e) {
            // 回车键事件
            if (e.which === 13) {
                coachPage(1);
            }
        });
        //拖动排序
        $(".is-drag").sortable({
            cursor: "move",
            items: ".is-drag-li",               //只是li可以拖动
            opacity: 0.6,                       //拖动时，透明度为0.6
            revert: false,                       //释放时，增加动画
            update: function (event, ui) {       //更新排序之后
                console.log($(this).sortable("toArray"));
            }
        });
    });

    //分页方法
    function coachPage(currentIndex) {
        var param = {
            "pageIndex": currentIndex,
            "pageSize": 10,
            "id": $("#id").val(),
            "nickName": $("#nickName").val(),
            "mobile": $("#mobile").val(),
            "storeId": $("#storeId").val(),
            "workType": $("#workType").val(),
            "status": $("#status").val(),
            "type" : 1
        };

        $("#coachList").empty();
        $.post(
            "${baseUrl}system/coach/coach_list",
            param,
            function (result) {

                var html = "";
                //分页
                /*new Page({
                    parent: $("#copot-page"),
                    nowPage: currentIndex,
                    pageSize: 10,
                    totalCount: result.coachCount,
                    setting: {
                        defaultPage: 5,//默认显示可点击分页的个数
                        firstPageText: "首页",
                        prevPageText: "上一页",
                        nextPageText: "下一页",
                        lastPageText: "尾页"
                    },
                    callback: function (nowPage, totalPage) {
                        coachPage(nowPage);
                    }
                });*/

                $("#coachCount").text(result.coachCount);

                var workType = null;
                var storeName = null;
                var status = null;
                $.each(result.coachList, function (i, data) {
//                    debugger
                    if (data.workType) {
                        if (data.workType == 1) {
                            workType = "全职";
                        } else if (data.workType ==2){
                            workType = "兼职";
                        } else {
                            workType = "无数据";
                        }
                    } else {
                        workType = "无数据";
                    }
                    if (data.storeName) {
                        storeName = data.storeName;
                    } else {
                        storeName = "";
                    }
                    if (data.status) {
                        if (data.status == 1) {
                            status = "在职";
                        }
                        if (data.status == 2) {
                            status = "离职";
                        }
                    }
                    html += "<tr class='is-drag-li'>";
                    html += "<td id='hf_" + data.id + "_" + (i + 1) + "'>" + (i + 1) + "</td>";
                    html += "<td>" + data.id + "</td>";
                    html += "<td>" + data.nickName + "</td>";
                    html += "<td>" + data.mobile + "</td>";
                    html += "<td>" + workType + "</td>";
                    html += "<td>" + data.joinTime + "</td>";
                    html += "<td>" + storeName + "</td>";
                    html += "<td>" + status + "</td>";
                    html += "<td width='270'>";
                    html += "<button type='button' onclick='editCoach(" + data.id + ")' class='btn btn-default'><i class='fa fa-pencil'></i> 编辑</button>";
                    html += "<button type='button' onclick='classFeeCoach(" + data.id + ")' class='btn btn-default'><i class='fa fa-pie-chart'></i> 课时费</button>";
                    if (data.status == 1) {
                        html += "<button type='button' onclick='offlineCoach(" + data.id + ")' class='btn btn-default'><i class='fa fa-arrow-circle-down'></i> 离职</button>";
                    }
                    html += "</td>";
                    html += "</tr>";
                });
                $("#coachList").html(html);
            }

        );
    }

    //教练课时费列表
    function classFeeCoach(id) {
       window.location = "${baseUrl}system/coach/coachCourseFeeList?coachId="+id;
    }
    //离职
    function offlineCoach(id) {

        swal({
            title: "确定要离职吗?",
            text: "离职后不可恢复!",
            type: "question",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            cancelButtonText: "容我三思！",
            confirmButtonText: "离职!",
        }).then(function () {
            $.post("coach_offline", {id: id}, function (result) {
                if (result) {
                    swal("离职成功!", "页面即将重新加载,请稍后...", "success");
                    window.setTimeout(function () {
                        window.location.reload()
                    }, 1000);
                }
            });
        },function () {
        });
    }
    //删除
    function deleteCoach(id) {

        swal({
            title: "确定要删除吗?",
            text: "删除后不可恢复!",
            type: "question",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            cancelButtonText: "容我三思！",
            confirmButtonText: "删除!"
        }).then(function () {
            $.post("coach_delete", {id: id}, function (result) {
                if (result) {
                    swal("删除成功!", "页面即将重新加载,请稍后...", "success");
                    window.setTimeout(function () {
                        window.location.reload()
                    }, 1000);
                }
            });
        },function () {
        });
    }

    /**
     * 私教教练排序
     * @param id listId
     * @author blank 2017/8/7 下午4:27
     */
    function sort(id) {
        var sort_string = "";
        $("#"+ id +"").find("td").each(function () {
            var val = $(this).attr("id");
            sort_string = sort_string + "##" + val;
        });
        $.ajax({
            url: "sort",
            data: "sortString=" + sort_string,
            type: "POST",
            success: function (result) {
                if (result) {
                    swal({
                        title:"排序修改成功!",
                        timer:1000
                    }).then(
                        function () {},
                        // handling the promise rejection
                        function (dismiss) {
                            if (dismiss === 'timer') {
//                                console.log('I was closed by the timer')
                            }
                        }
                    );
                    coachPage(1);
                }
            }
        });
    }

</script>

</@body.page>
</html>